package io.github.definitlyevil.ziminmod.listeners;

import io.github.definitlyevil.customitemframework.CIFItemInstance;
import io.github.definitlyevil.customitemframework.CustomItemFramework;
import io.github.definitlyevil.ziminmod.cif.armors.ZiMinHelmet;
import io.github.definitlyevil.ziminmod.cif.armors.ZiMinLeggings;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.ItemStack;

public class ZiMinLeggingsListener implements Listener {

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onDamage(EntityDamageByEntityEvent event) {
        if(!checkLeggings(event.getEntity())) return;
        if(!LivingEntity.class.isAssignableFrom(event.getDamager().getClass())) return;

        LivingEntity living = (LivingEntity) event.getDamager();
        living.sendMessage("\u00a7c你受到了针男人裤头的被动效果，受到了你自己50%的伤害！");
        living.damage(event.getFinalDamage() * 0.5f);
    }

    /**
     * 检查穿的是不是针男人裤头
     * @param entity
     * @return
     */
    private static boolean checkLeggings(Entity entity){
        if(entity == null || !LivingEntity.class.isAssignableFrom(entity.getClass())) return false;
        LivingEntity living = (LivingEntity) entity;
        if(living.getEquipment() == null || living.getEquipment().getLeggings() == null) return false;
        // 获取这个实体的头盔
        ItemStack itemStack = living.getEquipment().getLeggings();
        CIFItemInstance instance = CustomItemFramework.getCIFDataFromItem(itemStack);
        return instance != null && ZiMinLeggings.class.isAssignableFrom(instance.type.getClass());
    }

}
